//
//  HWCircleViewController.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/30.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView

class HWCircleViewController: HWBaseViewController {
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    //    var animationView = AnimationView()
    var animationView = LOTAnimationView()
    var titles:[String] = Array()
    var types:[NSInteger] = Array()
    var tagModel:HWGameCircleConfigModel?
    var httpManager = HWGameCircleHttpManager()
    
    let gameCircle = HWGameCircleController()
    let video = HWVideoshipingViewController()
    let gameLive = HWGameLiveController()
    /// 刷新数据
    var refreshButton:UIButton!
    var selectLive = false
    var selectCircle = false
    
    var statusBarStyle:UIStatusBarStyle = .default
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        let _ = super.preferredStatusBarStyle
        return self.statusBarStyle
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
        loadData()
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //        animationView.pause()
        //        animationView.play { success in
        
        //        }
        
        
        
        guard titles.count > 0 else {
            return
        }
        
        if selectLive == true {
            segmentedView.selectItemAt(index: 1)
            selectLive = false
        }
        
        if selectCircle == true {
            segmentedView.selectItemAt(index: 0)
            selectCircle = false
        }
       
        if types[segmentedView.selectedIndex] == 3 {
            self.statusBarStyle = .lightContent
            setNeedsStatusBarAppearanceUpdate()
            
            view.backgroundColor = .black
            segmentedDataSource.titleNormalColor = .white
            segmentedDataSource.titleSelectedColor = .white
            segmentedView.reloadData()
            self.cyl_tabBarController.tabBar.barTintColor = .colorWithHexString(hexString: "181818")
            
            if #available(iOS 15.0, *)  {
                let appearance = UITabBarAppearance()
                appearance.configureWithOpaqueBackground()
                appearance.backgroundColor = .colorWithHexString(hexString: "181818")
                self.cyl_tabBarController.tabBar.standardAppearance = appearance
                self.cyl_tabBarController.tabBar.scrollEdgeAppearance = appearance
                
            }
        }else{
            view.backgroundColor = .white
            segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#444444")
            segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#333333")
            segmentedView.reloadData()
            
            self.cyl_tabBarController.tabBar.barTintColor = .white
            if #available(iOS 15.0, *)  {
                let appearance = UITabBarAppearance()
                appearance.configureWithOpaqueBackground()
                appearance.backgroundImage = UIImage()
                appearance.shadowImage = UIColor.createImageWithColor(color: .white)
                appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#888888"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
                appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#FFB60A"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
                
                appearance.inlineLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#888888"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
                appearance.inlineLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#FFB60A"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
                
                appearance.compactInlineLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#888888"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
                appearance.compactInlineLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#FFB60A"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
                self.cyl_tabBarController.tabBar.standardAppearance = appearance
                self.cyl_tabBarController.tabBar.scrollEdgeAppearance = appearance
                UITabBar.appearance().tintColor = UIColor.colorWithHexString(hexString: "#FFB60A")
                UITabBar.appearance().layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#C4C4C4", alpha: 0.16).cgColor
                UITabBar.appearance().layer.shadowOffset = CGSize(width: 0, height: -3)
                UITabBar.appearance().layer.shadowOpacity = 1
                UITabBar.appearance().layer.shadowRadius = 6
                
            }
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.cyl_tabBarController.tabBar.barTintColor = .white
        if #available(iOS 15.0, *)  {
            let appearance = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundImage = UIImage()
            appearance.shadowImage = UIColor.createImageWithColor(color: .white)
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#888888"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#FFB60A"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
            
            appearance.inlineLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#888888"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
            appearance.inlineLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#FFB60A"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
            
            appearance.compactInlineLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#888888"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
            appearance.compactInlineLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.colorWithHexString(hexString: "#FFB60A"), NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]
            self.cyl_tabBarController.tabBar.standardAppearance = appearance
            self.cyl_tabBarController.tabBar.scrollEdgeAppearance = appearance
            UITabBar.appearance().tintColor = UIColor.colorWithHexString(hexString: "#FFB60A")
            UITabBar.appearance().layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#C4C4C4", alpha: 0.16).cgColor
            UITabBar.appearance().layer.shadowOffset = CGSize(width: 0, height: -3)
            UITabBar.appearance().layer.shadowOpacity = 1
            UITabBar.appearance().layer.shadowRadius = 6
        }
    }
}

extension HWCircleViewController {
    private func initUI() -> Void {
        navigationView.isHidden = true
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = []
        
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#444444")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#333333")
        segmentedDataSource.titleNormalFont = .systemFont(ofSize: 14*widthScale)
        segmentedDataSource.titleSelectedFont = .boldSystemFont(ofSize: 18*widthScale)
        segmentedDataSource.isItemSpacingAverageEnabled = false
        segmentedDataSource.itemSpacing = 30*widthScale
        segmentedDataSource.isItemWidthZoomEnabled = false
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorImageView()
        indicator.image = UIImage(named: "homeIndicator")
        indicator.indicatorHeight = 4*widthScale
        indicator.indicatorWidth = 20*widthScale
        indicator.verticalOffset = 0
        segmentedView.indicators = [indicator]
        segmentedView.delegate = self
        segmentedView.backgroundColor = .clear
        view.addSubview(self.segmentedView)
        
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        
        //        let animation = Animation.named("music")
        //        //      animation?.startFrame = CGRect(x: 10, y: 10, width: 10, height: 21)
        //        animationView.animation = animation
        //        animationView.loopMode = .loop
        //        animationView.contentMode = .scaleToFill
        //        animationView.backgroundColor = .colorWithHexString(hexString: "#FF1E00")
        //        animationView.layer.cornerRadius = 2
        //        animationView.respectAnimationFrameRate = true
        //
        //        let redValueProvider = ColorValueProvider(Color(r: 1, g: 1, b: 1, a: 1))
        //        for i in 0..<5 {
        //            let fillLineKeypath = AnimationKeypath(keypath: "line\(i+1).组 1.填充 1.Color")
        //            /// Set the provider on the animationView.
        //            animationView.setValueProvider(redValueProvider, keypath: fillLineKeypath)
        //            //
        //            let fillDotKeypath = AnimationKeypath(keypath: "dot\(i+1).组 1.填充 1.Color")
        //            /// Set the provider on the animationView.
        //            animationView.setValueProvider(redValueProvider, keypath: fillDotKeypath)
        //        }
        //        animationView.logHierarchyKeypaths()
        //        view.addSubview(animationView)
        //        animationView.isHidden = true
        
        let animation = LOTAnimationView(name: "music")
        animation.frame = CGRect(x: 10, y: 10, width: 10, height: 21)
        animation.loopAnimation = true
        animation.backgroundColor = .colorWithHexString(hexString: "#FF1E00")
        //        LOTKeypath *keypath = [LOTKeypath keypathWithKeys:@"Layer 1", @"Ellipse 1", @"Stroke 1", @"Color", nil];
        //        LOTColorValueCallback *colorCallback = [LOTColorBlockCallback withColor:[UIColor redColor]];
        //        [animationView setValueDelegate:colorCallback forKeypath:keypath];
        //        for i in 0..<5 {
        //            let keyPath = LOTKeypath.value(forKeyPath: "line\(i+1).组 1.填充 1.Color")
        //            let color:LOTColorBlockCallback = LOTColorBlockCallback { currentFrame, startKeyFrame, endKeyFrame, interpolatedProgress, var startColor, var endColor, var interpolatedColor in
        //                startColor = UIColor.red.cgColor
        //                endColor = UIColor.red.cgColor
        //                interpolatedColor = UIColor.red.cgColor
        //            }
        //            animation.setValueDelegate(color, for: keyPath as! LOTKeypath)
        ////            let fillLineKeypath = AnimationKeypath(keypath: "line\(i+1).组 1.填充 1.Color")
        //            /// Set the provider on the animationView.
        ////            animationView.setValueProvider(redValueProvider, keypath: fillLineKeypath)
        //
        //            //
        ////            let fillDotKeypath = AnimationKeypath(keypath: "dot\(i+1).组 1.填充 1.Color")
        ////            /// Set the provider on the animationView.
        ////            animationView.setValueProvider(redValueProvider, keypath: fillDotKeypath)
        //        }
        animation.logHierarchyKeypaths()
        view.addSubview(animation)
        animationView = animation
        animationView.play()
        animationView.isHidden = true
        
        refreshButton = UIButton(type: .custom)
        refreshButton.frame = CGRect(x: 0, y: 0, width:100*widthScale, height: 40*widthScale)
        refreshButton.setTitle("点击重试", for: .normal)
        refreshButton.setTitleColor(.white, for: .normal)
        refreshButton.titleLabel?.font = .boldSystemFont(ofSize: 14*widthScale)
        refreshButton.layer.cornerRadius = 20*widthScale
        refreshButton.layer.masksToBounds = true
        refreshButton.addTarget(self, action: #selector(refreshButtonClick(_:)), for: .touchUpInside)
        UIColor.gradientColor(view: refreshButton, colors: [UIColor.colorWithHexString(hexString: "#FFB60A").cgColor,UIColor.colorWithHexString(hexString: "#F66A05").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        view.addSubview(refreshButton)
        refreshButton.isHidden = true
    }
    
    private func initFrame() -> Void {
        segmentedView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalToSuperview()
            if screenHeight < 812 {
                make.top.equalTo(40*widthScale)
            }else{
                make.top.equalTo(60*widthScale)
            }
            make.height.equalTo(30*widthScale)
        }
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(segmentedView.snp.bottom).offset(10*widthScale)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
        }
        
        animationView.snp.makeConstraints { make in
            make.left.equalTo(151*widthScale)
            make.width.height.equalTo(14*widthScale)
            make.centerY.equalTo(segmentedView)
        }
        
        refreshButton.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(100*widthScale)
            make.height.equalTo(40*widthScale)
        }
    }
}
extension HWCircleViewController:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        
        if types[index] == 1 {
            return gameCircle
        }else if types[index] == 2 {
            return gameLive
        }else if types[index] == 3 {
            return video as! JXSegmentedListContainerViewListDelegate
        }else {
            return gameCircle
        }
        
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        if types[index] == 3 {
            self.statusBarStyle = .lightContent
            setNeedsStatusBarAppearanceUpdate()
            view.backgroundColor = .black
            segmentedDataSource.titleNormalColor = .white
            segmentedDataSource.titleSelectedColor = .white
            segmentedView.reloadData()
            self.cyl_tabBarController.tabBar.barTintColor = .colorWithHexString(hexString: "181818")
            
            if #available(iOS 15.0, *)  {
                let appearance = UITabBarAppearance()
                //                appearance.configureWithOpaqueBackground()
                appearance.backgroundColor = .colorWithHexString(hexString: "181818")
                self.cyl_tabBarController.tabBar.standardAppearance = appearance
                self.cyl_tabBarController.tabBar.scrollEdgeAppearance = appearance
                
            }
        }else{
            self.statusBarStyle = .default
            setNeedsStatusBarAppearanceUpdate()
            view.backgroundColor = .white
            segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#444444")
            segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#333333")
            segmentedView.reloadData()
            
            self.cyl_tabBarController.tabBar.barTintColor = .white
            if #available(iOS 15.0, *)  {
                let appearance = UITabBarAppearance()
                appearance.configureWithOpaqueBackground()
                appearance.backgroundImage = UIImage()
                appearance.shadowImage = UIColor.createImageWithColor(color: .white)
                self.cyl_tabBarController.tabBar.standardAppearance = appearance
                self.cyl_tabBarController.tabBar.scrollEdgeAppearance = appearance
                UITabBar.appearance().tintColor = UIColor.colorWithHexString(hexString: "#FFB60A")
                UITabBar.appearance().layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#C4C4C4", alpha: 0.16).cgColor
                UITabBar.appearance().layer.shadowOffset = CGSize(width: 0, height: -3)
                UITabBar.appearance().layer.shadowOpacity = 1
                UITabBar.appearance().layer.shadowRadius = 6
                
            }
        }
    }
    
}

extension HWCircleViewController {
    func loadData() -> Void {
        httpManager.getGameCircleConfig { model in
            self.refreshButton.isHidden = true
            self.titles.removeAll()
            self.types.removeAll()
            self.animationView.isHidden = true
            for mod in model.index_top {
                if mod.status == true {
                    self.titles.append(mod.name)
                    self.types.append(mod.type)
                    if mod.type == 2 {
                        self.animationView.isHidden = false
                    }
                }
            }
            self.tagModel = model
            self.segmentedDataSource.titles = self.titles
            self.segmentedView.reloadData()
            if self.selectLive == true {
                self.segmentedView.selectItemAt(index: 1)
                self.selectLive = false
            }
            
            if self.selectCircle == true {
                self.segmentedView.selectItemAt(index: 0)
                self.selectCircle = false
            }
            
        } failure: { error in
            self.refreshButton.isHidden = false
        }
    }
    
}

extension HWCircleViewController {
    @objc func refreshButtonClick(_:UIButton) -> Void {
        loadData()
    }
}
